//
//  JwDevice.m
//  JwPack
//
//  Created by 陈警卫 on 2024-07-31.
//

#import "JwDevice.h"
#import <sys/utsname.h>
#import "JwFile.h"

@implementation JwDevice

/// 获取APP显示名称
+ (NSString *)displayName {
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
}

/// 获取APP Bundle ID
+ (NSString *)bundleID {
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
}

/// 获取APP版本
+ (NSString *)versionShort {
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}

/// 获取APP构建ID
+ (NSString *)buildID {
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
}

/// 获取系统版本
+ (NSString *)systemVersion {
    return [[UIDevice currentDevice] systemVersion];
}

/// 获取系统名称
+ (NSString *)systemName {
    return [[UIDevice currentDevice] systemName];
}

/// 获取设备名称
+ (NSString *)devName {
    return [[UIDevice currentDevice] name];
}

/// 获取设备型号
+ (NSString *)devModel {
    return [[UIDevice currentDevice] model];
}

/// 获取设备本地化型号
+ (NSString *)localizedModel {
    return [[UIDevice currentDevice] localizedModel];
}

/// 计算设备硬盘空间总量
+ (int64_t)diskSpace {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSDictionary *attrs = [fileManager attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
    if (attrs != nil && error == nil) {
        NSNumber *space = attrs[NSFileSystemSize];
        if (space != nil) {
            return [space longLongValue];
        }
    }
    return 0;
}

/// 计算设备硬盘剩余空间
+ (int64_t)diskFreeSpace {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSDictionary *attrs = [fileManager attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
    if (attrs != nil && error == nil) {
        NSNumber *freeSpace = attrs[NSFileSystemFreeSize];
        if (freeSpace != nil) {
            return [freeSpace longLongValue];
        }
    }
    return 0;
}

/// 计算设备硬盘已使用空间
+ (int64_t)diskUsedSpace {
    int64_t totalSpace = [self diskSpace];
    int64_t freeSpace = [self diskFreeSpace];
    int64_t usedSpace = totalSpace - freeSpace;
    return usedSpace > 0 ? usedSpace : 0;
}


/// 获取缓存大小
+ (NSString *)cacheSize_MB {
    CGFloat size = [self cacheSize];
    return [NSString stringWithFormat:@"%.2fM", size/1024/1024];
}

/// 获取缓存大小
+ (CGFloat)cacheSize {
    // 得到缓存路径
    NSString *cachePath = [JwFile cachesPath];
    NSString *temPath = [JwFile tmpPath];
    NSFileManager *manager = [NSFileManager defaultManager];
    CGFloat size = 0;
    // 首先判断是否存在缓存文件
    if ([manager fileExistsAtPath:cachePath]) {
        NSArray *files = [manager subpathsAtPath:cachePath];
        for (NSString *fileName in files) {
            // 缓存文件绝对路径
            NSString *absolutPath = [cachePath stringByAppendingPathComponent:fileName];
            size = size + [manager attributesOfItemAtPath:absolutPath error:nil].fileSize;
        }
    }
    if ([manager fileExistsAtPath:temPath]) {
        NSArray *files = [manager subpathsAtPath:temPath];
        for (NSString *fileName in files) {
            // 缓存文件绝对路径
            NSString *absolutPath = [temPath stringByAppendingPathComponent:fileName];
            size = size + [manager attributesOfItemAtPath:absolutPath error:nil].fileSize;
        }
    }
    // 计算sdwebimage的缓存和系统缓存总和
    // size = size + [SDImageCache sharedImageCache].totalDiskSize;
    return size;
}

/// 清除缓存
+ (void)cleanCache {
    // 得到缓存路径
    NSString *cachePath = [JwFile cachesPath];
    NSString *temPath = [JwFile tmpPath];
    NSFileManager *manager = [NSFileManager defaultManager];
    // 判断是否存在缓存文件
    if ([manager fileExistsAtPath:cachePath]) {
        NSArray *files = [manager subpathsAtPath:cachePath];
        // 逐个删除缓存文件
        for (NSString *fileName in files) {
            NSString * absolutPat = [cachePath stringByAppendingPathComponent:fileName];
            [manager removeItemAtPath:absolutPat error:nil];
        }
    }
    if ([manager fileExistsAtPath:temPath]) {
        NSArray *files = [manager subpathsAtPath:temPath];
        // 逐个删除缓存文件
        for (NSString *fileName in files) {
            NSString * absolutPat = [temPath stringByAppendingPathComponent:fileName];
            [manager removeItemAtPath:absolutPat error:nil];
        }
    }
    // 删除sdwebimage的缓存
    // [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
}























/// 设备型号
+ (NSString *)phoneModel {
    struct utsname systemInfo;
    if (uname(&systemInfo) < 0) {
        return @"Unknown";
    } else {
        // 获取设备标识Identifier
        NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
        // 根据identifier去匹配到对应的型号名称
        NSString *modelName = [[self phoneGeneration] objectForKey:platform];
        return modelName ?: @"Unknown";
    }
}

/// 设备identifier对应name
/// https://www.theiphonewiki.com/wiki/Models
+ (NSDictionary *)phoneGeneration {
    return @{
        // iPhone
        @"iPhone1,1" : @"iPhone",
        @"iPhone1,2" : @"iPhone 3G",
        @"iPhone2,1" : @"iPhone 3GS",
        @"iPhone3,1" : @"iPhone 4",
        @"iPhone3,2" : @"iPhone 4",
        @"iPhone3,3" : @"iPhone 4",
        @"iPhone4,1" : @"iPhone 4S",
        @"iPhone5,1" : @"iPhone 5",
        @"iPhone5,2" : @"iPhone 5",
        @"iPhone5,3" : @"iPhone 5c",
        @"iPhone5,4" : @"iPhone 5c",
        @"iPhone6,1" : @"iPhone 5s",
        @"iPhone6,2" : @"iPhone 5s",
        @"iPhone7,2" : @"iPhone 6",
        @"iPhone7,1" : @"iPhone 6 Plus",
        @"iPhone8,1" : @"iPhone 6s",
        @"iPhone8,2" : @"iPhone 6s Plus",
        @"iPhone8,4" : @"iPhone SE (1st generation)",
        @"iPhone9,1" : @"iPhone 7",
        @"iPhone9,3" : @"iPhone 7",
        @"iPhone9,2" : @"iPhone 7 Plus",
        @"iPhone9,4" : @"iPhone 7 Plus",
        @"iPhone10,1" : @"iPhone 8",
        @"iPhone10,4" : @"iPhone 8",
        @"iPhone10,2" : @"iPhone 8 Plus",
        @"iPhone10,5" : @"iPhone 8 Plus",
        @"iPhone10,3" : @"iPhone X",
        @"iPhone10,6" : @"iPhone X",
        @"iPhone11,8" : @"iPhone XR",
        @"iPhone11,2" : @"iPhone XS",
        @"iPhone11,6" : @"iPhone XS Max",
        @"iPhone11,4" : @"iPhone XS Max",
        @"iPhone12,1" : @"iPhone 11",
        @"iPhone12,3" : @"iPhone 11 Pro",
        @"iPhone12,5" : @"iPhone 11 Pro Max",
        @"iPhone12,8" : @"iPhone SE (2nd generation)",
        @"iPhone13,1" : @"iPhone 12 mini",
        @"iPhone13,2" : @"iPhone 12",
        @"iPhone13,3" : @"iPhone 12 Pro",
        @"iPhone13,4" : @"iPhone 12 Pro Max",
        @"iPhone14,4" : @"iPhone 13 mini",
        @"iPhone14,5" : @"iPhone 13",
        @"iPhone14,2" : @"iPhone 13 Pro",
        @"iPhone14,3" : @"iPhone 13 Pro Max",
        @"iPhone14,6" : @"iPhone SE (3rd generation)",
        @"iPhone14,7" : @"iPhone 14",
        @"iPhone14,8" : @"iPhone 14 Plus",
        @"iPhone15,2" : @"iPhone 14 Pro",
        @"iPhone15,3" : @"iPhone 14 Pro Max",
        // iPad
        @"iPad1,1" : @"iPad",
        @"iPad2,1" : @"iPad 2",
        @"iPad2,2" : @"iPad 2",
        @"iPad2,3" : @"iPad 2",
        @"iPad2,4" : @"iPad 2",
        @"iPad3,1" : @"iPad (3rd generation)",
        @"iPad3,2" : @"iPad (3rd generation)",
        @"iPad3,3" : @"iPad (3rd generation)",
        @"iPad3,4" : @"iPad (4th generation)",
        @"iPad3,5" : @"iPad (4th generation)",
        @"iPad3,6" : @"iPad (4th generation)",
        @"iPad6,11" : @"iPad (5th generation)",
        @"iPad6,12" : @"iPad (5th generation)",
        @"iPad7,5" : @"iPad (6th generation)",
        @"iPad7,6" : @"iPad (6th generation)",
        @"iPad7,11" : @"iPad (7th generation)",
        @"iPad7,12" : @"iPad (7th generation)",
        @"iPad11,6" : @"iPad (8th generation)",
        @"iPad11,7" : @"iPad (8th generation)",
        @"iPad12,1" : @"iPad (9th generation)",
        @"iPad12,2" : @"iPad (9th generation)",
        @"iPad4,1" : @"iPad Air",
        @"iPad4,2" : @"iPad Air",
        @"iPad4,3" : @"iPad Air",
        @"iPad5,3" : @"iPad Air 2",
        @"iPad5,4" : @"iPad Air 2",
        @"iPad11,3" : @"iPad Air (3rd generation)",
        @"iPad11,4" : @"iPad Air (3rd generation)",
        @"iPad13,1" : @"iPad Air (4th generation)",
        @"iPad13,2" : @"iPad Air (4th generation)",
        @"iPad13,16" : @"iPad Air (5th generation)",
        @"iPad13,17" : @"iPad Air (5th generation)",
        @"iPad6,7" : @"iPad Pro (12.9-inch)",
        @"iPad6,8" : @"iPad Pro (12.9-inch)",
        @"iPad6,3" : @"iPad Pro (9.7-inch)",
        @"iPad6,4" : @"iPad Pro (9.7-inch)",
        @"iPad7,1" : @"iPad Pro (12.9-inch) (2nd generation)",
        @"iPad7,2" : @"iPad Pro (12.9-inch) (2nd generation)",
        @"iPad7,3" : @"iPad Pro (10.5-inch)",
        @"iPad7,4" : @"iPad Pro (10.5-inch)",
        @"iPad8,1" : @"iPad Pro (11-inch)",
        @"iPad8,2" : @"iPad Pro (11-inch)",
        @"iPad8,3" : @"iPad Pro (11-inch)",
        @"iPad8,4" : @"iPad Pro (11-inch)",
        @"iPad8,5" : @"iPad Pro (12.9-inch) (3rd generation)",
        @"iPad8,6" : @"iPad Pro (12.9-inch) (3rd generation)",
        @"iPad8,7" : @"iPad Pro (12.9-inch) (3rd generation)",
        @"iPad8,8" : @"iPad Pro (12.9-inch) (3rd generation)",
        @"iPad8,9" : @"iPad Pro (11-inch) (2nd generation)",
        @"iPad8,10" : @"iPad Pro (11-inch) (2nd generation)",
        @"iPad8,11" : @"iPad Pro (12.9-inch) (4th generation)",
        @"iPad8,12" : @"iPad Pro (12.9-inch) (4th generation)",
        @"iPad13,4" : @"iPad Pro (11-inch) (3rd generation)",
        @"iPad13,5" : @"iPad Pro (11-inch) (3rd generation)",
        @"iPad13,6" : @"iPad Pro (11-inch) (3rd generation)",
        @"iPad13,7" : @"iPad Pro (11-inch) (3rd generation)",
        @"iPad13,8" : @"iPad Pro (12.9-inch) (5th generation)",
        @"iPad13,9" : @"iPad Pro (12.9-inch) (5th generation)",
        @"iPad13,10" : @"iPad Pro (12.9-inch) (5th generation)",
        @"iPad13,11" : @"iPad Pro (12.9-inch) (5th generation)",
        @"iPad2,5" : @"iPad mini",
        @"iPad2,6" : @"iPad mini",
        @"iPad2,7" : @"iPad mini",
        @"iPad4,4" : @"iPad mini 2",
        @"iPad4,5" : @"iPad mini 2",
        @"iPad4,6" : @"iPad mini 2",
        @"iPad4,7" : @"iPad mini 3",
        @"iPad4,8" : @"iPad mini 3",
        @"iPad4,9" : @"iPad mini 3",
        @"iPad5,1" : @"iPad mini 4",
        @"iPad5,2" : @"iPad mini 4",
        @"iPad11,1" : @"iPad mini (5th generation)",
        @"iPad11,2" : @"iPad mini (5th generation)",
        @"iPad14,1" : @"iPad mini (6th generation)",
        @"iPad14,2" : @"iPad mini (6th generation)",
        // iTouch
        @"iPod1,1" : @"iTouch 1",
        @"iPod2,1" : @"iTouch 2",
        @"iPod3,1" : @"iTouch 3",
        @"iPod4,1" : @"iTouch 4",
        @"iPod5,1" : @"iTouch 5",
        @"iPod7,1" : @"iTouch 6",
        @"iPod9,1" : @"iTouch 7",
        // 其他
        @"i386" : @"iPhone Simulator",
        @"x86_64" : @"iPhone Simulator",
    };
}

@end
